from api.base import Login
from basic.parser_files import read_file


class MerchantProduct(Login):
    def __init__(self):
        super().__init__()
        self.merchant_token = None

    def product_list(self, page=1, limit=20, status='all', search_keyword=''):
        """
        商品列表
        :param page: 页码，默认为 1
        :param limit: 每页显示数量，默认为 20
        :param status: 商品状态，默认为'all'
        :param search_keyword: 搜索关键字，默认为空
        """
        data = {
            "method": "get",
            "url": "/api/admin/merchant/product/list",
            "headers": {'authori-zation': self.mer_token},
            "params": {
                'page': page,
                'limit': limit,
                'status': status,
                'searchType': 'all',
                'content': search_keyword,

            }
        }
        return self.send_mer_requests(self.product_list.__name__, data)

    def product_search(self, product_id=None, name=None, category=None):
        """
        商品查询
        :param product_id: 商品 ID，可选
        :param name: 商品名称，可选
        :param category: 商品类别，可选
        """
        params = {}
        if product_id:
            params['productId'] = product_id
        if name:
            params['name'] = name
        if category:
            params['category'] = category
        data = {
            "method": "get",
            "url": "/api/admin/merchant/product/search",
            "headers": {'authori-zation': self.merchant_token},
            "params": params
        }
        return self.send_mer_requests(self.product_search.__name__, data)

    def product_reset(self, product_id):
        """
        商品重置
        :param product_id: 商品 ID
        """
        data = {
            "method": "post",
            "url": f"/api/admin/merchant/product/reset/{product_id}",
            "headers": {"content-type": "application/json", 'authori-zation': self.merchant_token},
            "json": {}
        }
        return self.send_mer_requests(self.product_reset.__name__, data)

    def product_detail(self, product_id):
        """
        商品详情
        :param product_id: 商品 ID
        """
        data = {
            "method": "get",
            "url": f"/api/admin/merchant/product/detail/{product_id}",
            "headers": {'authori-zation': self.merchant_token}
        }
        return self.send_mer_requests(self.product_detail.__name__, data)

    def product_preview(self, product_id):
        """
        商品预览
        :param product_id: 商品 ID
        """
        data = {
            "method": "get",
            "url": f"/api/admin/merchant/product/preview/{product_id}",
            "headers": {'authori-zation': self.merchant_token}
        }
        return self.send_mer_requests(self.product_preview.__name__, data)


if __name__ == '__main__':
    obj = MerchantProduct()
    # 示例调用
    # 获取商品列表
    product_list_result = obj.product_list()
    print(product_list_result)
    # 查询商品
    product_search_result = obj.product_search(product_id=123)
    print(product_search_result)
    # 重置商品
    product_reset_result = obj.product_reset(product_id=123)
    print(product_reset_result)
    # 获取商品详情
    product_detail_result = obj.product_detail(product_id=123)
    print(product_detail_result)
    # 商品预览
    product_preview_result = obj.product_preview(product_id=123)
    print(product_preview_result)